package com.cby.library.data.remote.interceptor;


import com.cby.library.tool.CbyTools;
import com.cby.library.tool.util.AppUtils;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 描述信息
 *
 * @author chenbaoyang
 * @create 2018/12/25
 */
public class HttpParamsInterceptor implements Interceptor {
  private static final Charset UTF8 = Charset.forName("UTF-8");

  private static final String KEY_PREVIEW = "preview";

  private static final String KEY_DEVICE = "Device";

  private static final String KEY_VERSION_CODE = "VersionCode";
  private static final String KEY_VERSION_NAME = "VersionName";

  private static final String KEY_OS_VERSION = "OsVersion";

  private static final String KEY_PACKAGE_NAME = "PackageName";
   private static final String KEY_SDI = "Sdi";
  public static final String  KEY_VERSION = "v";
  public static final String  KEY_CHANNEL = "Channel";

  @Override
  public Response intercept(Chain chain) throws IOException {

    Request request = chain.request();

    Response response;
    try {
      //build new request
      Request.Builder requestBuilder = request.newBuilder()
         // .url(newUrl)
          .headers(buildNewHeader());

      //统一拼凑公共方法
      HttpUrl.Builder urlBuilder = request.url().newBuilder();
      urlBuilder.addQueryParameter(KEY_VERSION,  AppUtils.getVersionName(CbyTools.getApplication()));
      if (CbyTools.isPreviewMode()) {
        urlBuilder.addQueryParameter(KEY_PREVIEW,  "1");
      }
        // 将最终的url填充到request中
      requestBuilder.url(urlBuilder.build());

      response = chain.proceed(requestBuilder.build());

    } catch (Exception e) {
      throw e;
    }
    return response;
  }



  private Headers buildNewHeader() {
    //构建header信息
    Headers.Builder headerBuilder = new Headers.Builder();
    buildDeviceHeader(headerBuilder);
    buildUserHeader(headerBuilder);
    //预览模式需增加配置
    if (CbyTools.isPreviewMode()) {
      headerBuilder.set(KEY_PREVIEW, "1");
    }
    return headerBuilder.build();
  }

  /**
   * 设置设备信息header头
   * @param headerBuilder
   */
  private void buildDeviceHeader(Headers.Builder headerBuilder) {
    /*headerBuilder.set(KEY_DEVICE, URLEncoder.encode(DeviceUtils.getPhoneModel()));
    headerBuilder.set(KEY_OS_VERSION, DeviceUtils.getPhoneVersion());
    headerBuilder.set(KEY_SDI, DeviceUuidGenerator.getUdidInfo());
    headerBuilder.set(KEY_CHANNEL, AppUtils.getChannelName());*/
    String packageName = CbyTools.getApplication().getPackageName();
    headerBuilder.set(KEY_VERSION_CODE, "" + AppUtils.getAppVersionCode(CbyTools.getApplication()));
    headerBuilder.set(KEY_VERSION_NAME, AppUtils.getVersionName(CbyTools.getApplication()));
    headerBuilder.set(KEY_PACKAGE_NAME, packageName);
  }
  /**
   * 设置已登录的header头信息
   * @param headerBuilder
   */
  private void buildUserHeader( Headers.Builder headerBuilder) {

    //用户已登录，设置header头
    /*UserInfo userInfo = H5UserManager.getInstance().getUserInfo();
    if (userInfo != null && userInfo.getAuthKey() != null
        && userInfo.getAuthValue() != null) {
      headerBuilder.set(userInfo.getAuthKey(), userInfo.getAuthValue());
    }*/

  }
}
